package cmcciot.onenet.nbapi.sdk.entity;

import cmcciot.onenet.nbapi.sdk.config.Config;

import java.time.LocalDateTime;

/**
 * @author LHL
 * @date: 2018/8/1
 */
public class ReadCache extends CommonEntity {
    //命令开始生效时间，可选（不填时默认为OneNET当前时间），填写必须大于OneNET服务器的当前时间
    private LocalDateTime validTime;
    //命令过期时间，必填且大于valid_time
    private LocalDateTime expiredTime;
    //表示失败重试次数（等待下一次设备update或者上线）可选（不填时默认为3），填写时必须在[3, 10]之间
    private Integer retry;

    public void setObjInstId(Integer objInstId) {
        this.objInstId = objInstId;
    }

    public void setResId(Integer resId) {
        this.resId = resId;
    }

    public ReadCache(String imei, Integer objId, LocalDateTime expiredTime) {
        this.imei = imei;
        this.objId = objId;
        this.expiredTime = expiredTime;
    }

    public Integer getRetry() {
        return retry;
    }

    public void setRetry(Integer retry) {
        this.retry = retry;
    }

    public LocalDateTime getValidTime() {
        return validTime;
    }

    public void setValidTime(LocalDateTime validTime) {
        this.validTime = validTime;
    }

    public void setObjId() {
    }

    @Override
    public String toUrl() {
        StringBuilder url = new StringBuilder(Config.getDomainName());
        url.append("/nbiot/offline?imei=").append(this.imei);
        url.append("&obj_id=").append(this.objId);
        if (this.objInstId != null) {
            url.append("&obj_inst_id=").append(this.objInstId);
        }
        if (this.resId != null) {
            url.append("&res_id=").append(this.resId);
        }
        if (this.validTime != null) {
            url.append("&valid_time=").append(this.validTime.withNano(0).toString());
        }
        if (this.expiredTime != null) {
            url.append("&expired_time=").append(this.expiredTime.withNano(0).toString());
        }
        url.append("&retry=").append(this.retry == null ? 3 : this.retry);
        return url.toString();
    }

}
